home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / gnu / glibc108.gz / glibc108 / glibc-1.08.1 / time / offtime.c < prev    next >
C/C++ Source or Header  |  1993-04-21  |  2KB  |  85 lines

  1. /* Copyright (C) 1991, 1993 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3.  
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Library General Public License as
  6. published by the Free Software Foundation; either version 2 of the
  7. License, or (at your option) any later version.
  8.  
  9. The GNU C Library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  12. Library General Public License for more details.
  13.  
  14. You should have received a copy of the GNU Library General Public
  15. License along with the GNU C Library; see the file COPYING.LIB.  If
  16. not, write to the Free Software Foundation, Inc., 675 Mass Ave,
  17. Cambridge, MA 02139, USA.  */
  18.  
  19. #include <ansidecl.h>
  20. #include <time.h>
  21.  
  22.  
  23. /* Defined in mktime.c.  */
  24. extern CONST unsigned short int __mon_lengths[2][12];
  25.  
  26. #define    SECS_PER_HOUR    (60 * 60)
  27. #define    SECS_PER_DAY    (SECS_PER_HOUR * 24)
  28.  
  29. /* Returns the `struct tm' representation of *T,
  30.    offset OFFSET seconds east of UCT.    */
  31. struct tm *
  32. DEFUN(__offtime, (t, offset), CONST time_t *t AND long int offset)
  33. {
  34.   static struct tm tbuf;
  35.   register long int days, rem;
  36.   register int y;
  37.   register CONST unsigned short int *ip;
  38.  
  39.   if (t == NULL)
  40.     return NULL;
  41.  
  42.   days = *t / SECS_PER_DAY;
  43.   rem = *t % SECS_PER_DAY;
  44.   rem += offset;
  45.   while (rem < 0)
  46.     {
  47.       rem += SECS_PER_DAY;
  48.       --days;
  49.     }
  50.   while (rem >= SECS_PER_DAY)
  51.     {
  52.       rem -= SECS_PER_DAY;
  53.       ++days;
  54.     }
  55.   tbuf.tm_hour = rem / SECS_PER_HOUR;
  56.   rem %= SECS_PER_HOUR;
  57.   tbuf.tm_min = rem / 60;
  58.   tbuf.tm_sec = rem % 60;
  59.   /* January 1, 1970 was a Thursday.  */
  60.   tbuf.tm_wday = (4 + days) % 7;
  61.   if (tbuf.tm_wday < 0)
  62.     tbuf.tm_wday += 7;
  63.   y = 1970;
  64.   while (days >= (rem = __isleap(y) ? 366 : 365))
  65.     {
  66.       ++y;
  67.       days -= rem;
  68.     }
  69.   while (days < 0)
  70.     {
  71.       --y;
  72.       days += __isleap(y) ? 366 : 365;
  73.     }
  74.   tbuf.tm_year = y - 1900;
  75.   tbuf.tm_yday = days;
  76.   ip = __mon_lengths[__isleap(y)];
  77.   for (y = 0; days >= ip[y]; ++y)
  78.     days -= ip[y];
  79.   tbuf.tm_mon = y;
  80.   tbuf.tm_mday = days + 1;
  81.   tbuf.tm_isdst = -1;
  82.  
  83.   return &tbuf;
  84. }
  85.